﻿/*
 * Catalyst - http://catalystobjects.com
 * 
 * New BSD License (BSD)
 * 
 * Copyright (c) 2009, Ashley Brener
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of Midlet Earth nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using SubSonic;

namespace Catalyst
{
	[Serializable]
	public abstract class RelationalObject : ObjectBase
	{
		public abstract bool IsLoaded { get; }

		#region *** Schema ***

		public TableSchema.Table Schema;
		//protected DataProvider provider;

		public abstract TableSchema.Table GetTableSchema();

		private DataProvider provider;

		public TableSchema.Table CreateTableSchema(string ProviderName, string TableName)
		{
			if (provider == null)
			{
				provider = DataService.Providers[ProviderName];
			}

			return provider.GetTableSchema(TableName, getTableType(TableName));
		}

		//public abstract TableSchema GetTableSchema();
		//public abstract string GetProviderName();

		//protected void LoadSchema()
		//{
		//    loadSchema(GetTableName());
		//}

		//internal void loadSchema(string tableName)
		//{
		//    provider = DataService.Providers[GetProviderName()];

		//    Schema = provider.GetTableSchema(tableName, getTableType(provider, tableName));
		//}

		//private TableType getTableType(DataProvider provider, string tableName)
		private TableType getTableType(string tableName)
		{
			List<string> viewNames = new List<string>(provider.GetViewNameList());

			if (viewNames.Contains(tableName))
			{
				return TableType.View;
			}

			return TableType.Table;
		}

		#endregion

		#region *** Load Data ***

		internal abstract void LoadInternal(IDataReader Reader);
		#endregion

		#region *** RelationalObject Loading ***

		internal List<string> relationNamesToLoad;

		//public void LoadAsRelation(TableSchema.Table RelatedTable, string ForeignKeyName, object PrimaryKeyValue)
		//{
		//    LoadAsRelation(RelatedTable, ForeignKeyName, PrimaryKeyValue, null);
		//}

		public void LoadAsRelation(TableSchema.Table RelatedTableSchema, string ForeignKeyName, object PrimaryKeyValue, HashSet<Aggregate> Aggregates)
		{
			LoadAsRelation<RelationalObject>(RelatedTableSchema, ForeignKeyName, PrimaryKeyValue, Aggregates);
		}

		//public T LoadAsRelation<T>(string ForeignKeyName, object PrimaryKeyValue)
		//{
		//    return LoadAsRelation<T>(GetTableSchema(), ForeignKeyName, PrimaryKeyValue);
		//}

		//public T LoadAsRelation<T>(string ForeignKeyName, object PrimaryKeyValue, HashSet<Where> Constraints)
		//{
		//    return LoadAsRelation<T>(GetTableSchema(), ForeignKeyName, PrimaryKeyValue, Constraints);
		//}

		public T LoadAsRelation<T>(TableSchema.Table RelatedTableSchema, string ForeignKeyName, object PrimaryKeyValue)
		{
			return LoadAsRelation<T>(RelatedTableSchema, ForeignKeyName, PrimaryKeyValue, null);
		}

		public T LoadAsRelation<T>(TableSchema.Table RelatedTableSchema, string ForeignKeyName, object PrimaryKeyValue, HashSet<Aggregate> Aggregates)
		{
			//string tableName = TableName;

			//if (String.IsNullOrEmpty(tableName))
			//{
			//    tableName = GetTableName();
			//}

			//loadSchema(tableSchema);

			Schema = RelatedTableSchema;

			SqlQuery query = new Select()
			.From(Schema)
			.Where(ForeignKeyName).IsEqualTo(PrimaryKeyValue);
			
			if (Aggregates != null && Aggregates.Count > 0)
			{
				foreach (Aggregate aggregate in Aggregates)
				{
					query.And(aggregate);
				}
			}

			LoadInternal(query.ExecuteReader());

			return (T)Convert.ChangeType(this, this.GetType());
		}

		#endregion
	}
}
